package com.eascs.dubbo.api.internal;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.api.entity.AssetsTotInfo;
import com.eascs.admin.api.entity.ContactsInfo;
import com.eascs.admin.api.form.LinkPersonVo;
import com.eascs.admin.api.internal.SpouseInternalDetailService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.crm.nsEditDetail.dao.MainStoreInfoDao;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
@Service
public class SpouseInternalDetailImpl  implements SpouseInternalDetailService{
	@Autowired
	MainStoreInfoDao mainStoreInfoDao;
	@Override
	public PageResult<Object[]> searchLinkPerson(String loanapplyNo) {//t.Address, t.home_phone
		String sql="SELECT t.id, t.Contacts_name, t.Type, t.Address, t.home_phone FROM contacts_info t WHERE t.is_del = 0 AND t.loanapply_no =?";
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		pageQuery.addParam(loanapplyNo);
		pageQuery.setQuerySql(sql);
		return mainStoreInfoDao.findDataWithPage(pageQuery);
	}
	
	@Override
	public int updateLinkPerson(LinkPersonVo linkPersonVo) {
		User loginUser = UserHolder.getUser();
		ContactsInfo contactsInfo=mainStoreInfoDao.getById(ContactsInfo.class, linkPersonVo.getId());
		contactsInfo.setContacts_name(linkPersonVo.getName());
		contactsInfo.setAddress(linkPersonVo.getAddress());
		contactsInfo.setType(linkPersonVo.getRelationType());
		contactsInfo.setHomePhone(linkPersonVo.getPhone());
		contactsInfo.setUpdator(loginUser.getNo());
		contactsInfo.setUpdateTime(new Date());
		return mainStoreInfoDao.updateById(contactsInfo);
	}
	
	@Override
	public int deleteLinkPerson(String id) {
		String sql = "update contacts_info set is_del = 1 where id = ?";
		return mainStoreInfoDao.execute(sql, id);
	}
	
	@Override
	public int saveLinkPerson(LinkPersonVo linkPersonVo) {
		User loginUser = UserHolder.getUser();
		ContactsInfo contactsInfo = new ContactsInfo();
		contactsInfo.setLoanapplyNo(linkPersonVo.getLoanapplyNo());
		contactsInfo.setContacts_name(linkPersonVo.getName());
		contactsInfo.setAddress(linkPersonVo.getAddress());
		contactsInfo.setType(linkPersonVo.getRelationType());
		contactsInfo.setHomePhone(linkPersonVo.getPhone());
		contactsInfo.setIsDel("0");
		contactsInfo.setUpdator(loginUser.getNo());
		contactsInfo.setUpdateTime(new Date());
		int saveResult = mainStoreInfoDao.insert(contactsInfo);
		return saveResult;
	}
	
	@Override
	public AssetsTotInfo manAndWifeDebt(String loanapplyNo) {
		String sql="select * from assets_tot_info where loanapply_no = ?";
		return mainStoreInfoDao.findEntity(sql, AssetsTotInfo.class, loanapplyNo);
	}
	@Override
	public PageResult<Object[]> houseDetail(String loanapplyNo) {//h.address
		String sql="SELECT h.id, h.address, h.aypes, h.is_mortgage, log.valuation, h.house_Price, log.house_Price house_price_review FROM house_info h LEFT JOIN house_info_log log ON h.id = log.house_info_id AND log.op_date IN ( SELECT max(ll.op_date) FROM house_info_log ll WHERE ll.house_info_id = log.house_info_id) WHERE 1 = 1 AND h.is_del = 'N' AND h.loanapply_no =?";
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		pageQuery.addParam(loanapplyNo);
		pageQuery.setQuerySql(sql);
		return mainStoreInfoDao.findDataWithPage(pageQuery);
	}
	@Override
	public PageResult<Object[]> carDetail(String loanapplyNo) {
		String sql="SELECT h.id, h.car_name, h.car_amount, h.car_data, h.is_mortgage, log.valuation, h.car_price, log.Car_Price Car_Price_review FROM car_info h LEFT JOIN Car_info_log log ON h.id = log.Car_info_id AND log.op_date IN ( SELECT max(ll.op_date) FROM Car_info_log ll WHERE ll.Car_info_id = log.Car_info_id) WHERE 1 = 1 AND h.is_del = 'N' AND h.loanapply_no =?";
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		pageQuery.addParam(loanapplyNo);
		pageQuery.setQuerySql(sql);
		return mainStoreInfoDao.findDataWithPage(pageQuery);
	}
	@Override
	public PageResult<Object[]> otherProperty(String loanapplyNo) {
		String sql="SELECT h.id, h.other_type, h.other_price, log.other_price other_price_review FROM other_info h LEFT JOIN other_info_log log ON h.id = log.other_info_id AND log.op_date IN ( SELECT max(ll.op_date) FROM other_info_log ll WHERE ll.other_info_id = log.other_info_id) WHERE 1 = 1 AND h.is_del = 'N' AND h.loanapply_no =?";
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		pageQuery.addParam(loanapplyNo);
		pageQuery.setQuerySql(sql);
		return mainStoreInfoDao.findDataWithPage(pageQuery);
	}


}
